package org.sky.generator.database.analize;

import org.sky.generator.database.model.Column;
import org.sky.generator.database.model.Table;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author weifx
 * @Title: IAnalizeDatabase
 * @ProjectName springboot-platform
 * @Description: TODO{数据库分析}
 * @date 2019/2/25 23:34
 * @Version 1.0
 */
public interface IAnalizeDatabase {
    /**
     * 根据表名获取表信息
     * @param tableName
     * @return
     */
    public Table getTable(String tableName);

    /**
     * 根据表名获取字段信息
     * @param tableName
     * @return
     */
    public List<Column> getCloumns(String tableName);
    /**
     * ResultSet转数组
     * @param resultSet
     * @return
     * @throws SQLException
     */
     default List<Map> convertList(ResultSet resultSet) throws SQLException {
        List list = new ArrayList();
        ResultSetMetaData md = resultSet.getMetaData();
        int columnCount = md.getColumnCount();
        while (resultSet.next()) {
            Map rowData = new HashMap();
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), resultSet.getObject(i));
            }
            list.add(rowData);
        }
        return list;
    }
}
